package com.ztools.pc.async;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ztools.common.model.pc.access.Record;
import com.ztools.pc.service.access.RecordService;
import com.ztools.pc.util.IpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.ztools.common.util.common.UrlUtil.IP_URL;

/**
 * @Author: seeTools
 * @Date: 2023/4/28  15:57
 * @Version 1.0
 * 访问配置异步记录
 */
@Slf4j
@Async
@Component
public class AccessConfigAsync {

    @Resource
    private RecordService recordService;

    /**
     * 访问记录异步方法
     *
     * @param method 访问的方法名
     */
    @Async
    public void accessRecord(HttpServletRequest request, String method) {
        log.info("开始记录访问记录，访问方法为{}", method);
        String ip = IpUtil.getIp(request);
        QueryWrapper<Record> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Record::getAccessIp, ip).eq(Record::getAccessMethod, method);
        if (recordService.list(queryWrapper).size() > 0) {
            return;
        }
        Record record = new Record();
        // 请求获取IP详细信息的接口
        String result = HttpUtil.get(StrUtil.format(IP_URL, ip));
        log.info("调用获取ip接口,结果为：{}", result);
        if (JSONUtil.isJson(result)) {
            JSONObject jsonObject = JSONUtil.parseObj(result);
            String status = jsonObject.getStr("status");
            String data = jsonObject.getStr("data");
            JSONArray jsonArray = JSONUtil.parseArray(data);
            if ("0".equals(status) && jsonArray.size() > 0) {
                JSONObject jsonData = JSONUtil.parseObj(jsonArray.get(0));
                String appInfo = jsonData.getStr("appinfo");
                String displayType = jsonData.getStr("disp_type");
                String fetchKey = jsonData.getStr("fetchkey");
                String position = jsonData.getStr("location");
                String origIp = jsonData.getStr("origip");
                String resourceId = jsonData.getStr("resourceid");
                String roleId = jsonData.getStr("role_id");
                String titleCont = jsonData.getStr("titlecont");
                String tpLt = jsonData.getStr("tplt");
                record = Record.builder()
                        .accessIp(ip)
                        .appInfo(appInfo)
                        .fetchKey(fetchKey)
                        .position(position)
                        .tpLt(tpLt)
                        .origIp(origIp)
                        .titleCont(titleCont)
                        .accessMethod(method)
                        .build();
                if (StrUtil.isNotEmpty(roleId)) {
                    record.setRoleId(Integer.parseInt(roleId));
                }
                if (StrUtil.isNotEmpty(resourceId)) {
                    record.setResourceId(Integer.parseInt(resourceId));
                }
                if (StrUtil.isNotEmpty(displayType)) {
                    record.setDisplayType(Integer.parseInt(displayType));
                }
            } else {
                record.setAccessIp(ip);
            }
        }
        recordService.save(record);
    }
}
